package br.com.quad.managedBean.convert;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.quad.bean.Acessorio;
import br.com.quad.dao.DAOFactory;

@FacesConverter(value="converteAcessorio", forClass=Acessorio.class)
public class ConverteAcessorio implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		Acessorio acessorio;
		if(arg2.equals("0")){
			acessorio = new Acessorio();
		} else {
			acessorio = DAOFactory.getAcessorioDAO().getAcessorio(Integer.valueOf(arg2));
		}
		return acessorio;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		return String.valueOf(((Acessorio) arg2).getCodigoAcessorio());
		
	}
}
